package node_game

import (
	_ "sanguo/protocol/ss" //触发pb注册
	ss_rpc "sanguo/protocol/ss/rpc"
	"sanguo/rpc/synctoken"

	"github.com/golang/protobuf/proto"
)

var userTokenMap map[string]string

type SyncToken struct {
}

func (this *SyncToken) OnCall(replyer *synctoken.SynctokenReplyer, arg *ss_rpc.SynctokenReq) {
	user := arg.GetUserid()
	token := arg.GetToken()
	t, ok := userTokenMap[user]
	if !ok {
		userTokenMap[user] = token
	} else {
		token = t
	}
	replyer.Reply(&ss_rpc.SynctokenResp{
		Token: proto.String(token),
	})
}

func CheckToken(userID, token string) bool {
	t, ok := userTokenMap[userID]
	if !ok {
		return false
	} else {
		return t == token
	}
}

func RemoveToken(userID string) {
	delete(userTokenMap, userID)
}

func init() {
	userTokenMap = map[string]string{}
	synctoken.Register(&SyncToken{})
}
